package com.example.androidautoproject.hlxn

import android.util.Log
import com.example.androidautoproject.utils.AutoScript
import com.example.androidautoproject.utils.AdParam
import com.example.androidautoproject.utils.LogWrapper
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.findFirstParentClickable
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl

class HlxnProject: StepImpl() {
    private val TAG = "adProject"
    private var bool = false
    private var currentClassName = ""
    private var packageName = "com.dzsw.hlxn.plus"
    private var i = 0
    private var j = 0
    override fun onImpl(collector: StepCollector) {
        collector.next(stepTag = 1){
            AdParam.isStart = true
            AdParam.adLog = mutableListOf()
            AdParam.currentTag = "adProject"
            start_app()
            return@next Step.get(2)
        }.next(stepTag = 2){
            start_activity()
            return@next Step.get(3)
        }.next(stepTag = 3){
            start_ad()
            return@next Step.none
        }
    }

    suspend fun start_app(){
        Log.e(TAG, "1.开始${TAG}任务")
        LogWrapper.logAppend("1.开始${TAG}任务")
        AssistsCore.launchApp(packageName)
        Thread.sleep(15000 + AutoScript.ran_time)
        if(AutoScript.find_element_by_text("翻倍收益")) {
            Thread.sleep(1000 + AutoScript.ran_time)
            AutoScript.closeWinBySize(126, 126)
            Thread.sleep(2000 + AutoScript.ran_time)
        }
    }

    suspend fun start_activity(){
        Log.e(TAG, "2.开始点击赚钱任务")
        LogWrapper.logAppend("2.开始点击赚钱任务")
        var node = AssistsCore.findById("${packageName}:id/svga_task").firstOrNull()?.findFirstParentClickable()
        if(node != null){
            bool = true
            AutoScript.click_element(node)
            Thread.sleep(1000 + AutoScript.ran_time)
            node.recycle()
        } else {
            Log.e(TAG, "没有找到点击赚钱")
        }
        Thread.sleep(8000 + AutoScript.ran_time)
        AutoScript.click_element_by_text("立即签到")
    }

    suspend fun start_ad(){
        Log.e(TAG, "3.开始点击看广告赚钱")
        LogWrapper.logAppend("3.开始点击看广告赚钱")
        while(true){
            if(AdParam.isStart) {
                Thread.sleep(1000 + AutoScript.ran_time)
                if(AdParam.packageName == packageName){
                    check_ele()
                    Thread.sleep(1000 + AutoScript.ran_time)
                    check_ad()
                    Thread.sleep(1000 + AutoScript.ran_time)
                } else {
                    var node = AssistsCore.findById("com.huawei.appmarket:id/close_imageview").firstOrNull()
                    if(AdParam.packageName == "com.huawei.android.internal.app" && AdParam.className == "android.app.Dialog"){
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(300f, 2000f)
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AssistsCore.back()
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AutoScript.close_app()
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(600f, 1500f)
                    }else if(AdParam.packageName == "com.huawei.appmarket" && node != null){
                        Thread.sleep(3000+ AutoScript.ran_time)
                        AssistsCore.back()
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AutoScript.close_app()
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(600f, 1500f)
                        node.recycle()
                    } else {
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AssistsCore.recentApps()
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(600f, 1500f)
                    }
                    Thread.sleep(2000+ AutoScript.ran_time)
                    AdParam.className = currentClassName
                    Thread.sleep(3000+ AutoScript.ran_time)
                }
            }else{
                Log.e(TAG, "3.结束任务")
                LogWrapper.logAppend("3.结束任务")
                break
            }
        }
    }

    suspend fun check_ele(){
        if(AutoScript.find_element_by_text("下载安装视频")) {
            Thread.sleep(1000 + AutoScript.ran_time)
            AutoScript.closeWinBySize(78, 78)
            Thread.sleep(1000 + AutoScript.ran_time)
        }
        if(AutoScript.find_element_by_text("立即收下")) {
            Thread.sleep(2000 + AutoScript.ran_time)
            AutoScript.click_element_by_text("翻倍领取")
            Thread.sleep(3000 + AutoScript.ran_time)
            AutoScript.click_element_by_text("立即收下")
        }
        if(AutoScript.find_element_by_text("签到领元宝")) {
            if(AutoScript.click_element_by_text("看视频领元宝")){
                Thread.sleep(2000 + AutoScript.ran_time)
            }
           if( AutoScript.click_element_by_textAndIndex("领取奖励", 1)){
               Thread.sleep(2000 + AutoScript.ran_time)
               AutoScript.click_element_by_text("立即领取")
               Thread.sleep(2000 + AutoScript.ran_time)
           } else {
               Thread.sleep(2000 + AutoScript.ran_time)
               LogWrapper.logAppend("没有找到领取奖励")
           }
        }
    }
    suspend fun check_ad(){
        if(AdParam.className == "com.baidu.mobads.sdk.api.MobRewardVideoActivity" || AdParam.className.indexOf("com.baidu.mobads.sdk") >= 0){
            currentClassName = "com.baidu.mobads.sdk.api.MobRewardVideoActivity"
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(78, 78)
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(90, 90)
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySize(54, 54)
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(120, 138)
            Thread.sleep(1000+ AutoScript.ran_time)
            if(AutoScript.find_element_by_text("已领取")){
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("跳过")
            }
        }else if(AdParam.className == "com.sigmob.sdk.base.common.PortraitAdActivity"){
            currentClassName = "com.sigmob.sdk.base.common.PortraitAdActivity"
            Thread.sleep(1000+ AutoScript.ran_time)
            if(AutoScript.find_element_by_text("后获取奖励")){
                Thread.sleep(1000+ AutoScript.ran_time)
            } else if (AutoScript.find_element_by_text("继续看视频")) {
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("立即前往")
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("再次前往")
                Thread.sleep(1000+ AutoScript.ran_time)
            } else {
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(66, 66)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(69, 69)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(90, 90)
            }
        }else if(AdParam.className == "com.qumeng.advlib.ui.front.InciteADActivity" || AdParam.className.indexOf("com.qumeng.advlib") >= 0){
            currentClassName = "com.qumeng.advlib.ui.front.InciteADActivity"
            Thread.sleep(3000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(78, 78)
            Thread.sleep(3000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(90, 90)
            Thread.sleep(6000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(900, 84)
            Thread.sleep(3000+ AutoScript.ran_time)
            AutoScript.click_element_by_text("关闭")
            Thread.sleep(2000+ AutoScript.ran_time)
            AutoScript.click_element_by_text("残忍离开")
            Thread.sleep(1000+ AutoScript.ran_time)
        } else if(AdParam.className.indexOf("com.alimm.tanx.core.ad") >= 0){
            currentClassName = "com.alimm.tanx.core.ad"
            for(i in 1..5){
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.vertical_swipe()
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.vertical_swipe_up()
                Thread.sleep(4000+ AutoScript.ran_time)
            }
            Thread.sleep(1000+ AutoScript.ran_time)
        }  else if(AdParam.className.indexOf("com.maplehaze.adsdk") >= 0 || AdParam.className == "com.zhangyue.app.shortvideo.main.MainActivity"){
            currentClassName = "com.maplehaze.adsdk"
           if(AutoScript.find_element_by_text("后可领取奖励")){
               Thread.sleep(1000+ AutoScript.ran_time)
           } else {
               Thread.sleep(1000+ AutoScript.ran_time)
               AutoScript.closeWinBySizeOnly(75, 75)
               Thread.sleep(1000+ AutoScript.ran_time)
               AutoScript.closeWinBySizeOnly(105, 105)
               Thread.sleep(1000+ AutoScript.ran_time)
           }
        } else if(AdParam.className == "com.qq.e.ads.PortraitADActivity" || AdParam.className.indexOf("com.qq.e.ads") >= 0){
            currentClassName = "com.qq.e.ads"
            Thread.sleep(1000+ AutoScript.ran_time)
            if (AutoScript.find_element_by_text("恭喜获得奖励")) {
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(48, 48)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(132, 132)
            } else if (AutoScript.find_element_by_text("打开App体验15秒")) {
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.ran_click(540f, 2130f)
                Thread.sleep(14000+ AutoScript.ran_time)
            } else if (AutoScript.find_element_by_text("秒后发放")) {
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("放弃福利")
                Thread.sleep(1000+ AutoScript.ran_time)
//                if (AutoScript.find_element_by_text("我要更快拿奖")) {
//                    Thread.sleep(1000+ AutoScript.ran_time)
//                    AutoScript.click_element_by_text("我要更快拿奖")
//                    Thread.sleep(14000+ AutoScript.ran_time)
//                }
            } else {
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(48, 48)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(87, 105)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("抓住奖励机会")
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(37, 37)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.click_element_by_text("关闭广告")
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(126, 126)
                Thread.sleep(1000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(216, 42)
            }
        } else if(AdParam.className.indexOf("com.bytedance.sdk.openadsdk")>=0){
            currentClassName = "com.bytedance.sdk.openadsdk"
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(36, 36)
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(72, 66)
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(72, 72)
            Thread.sleep(1000+ AutoScript.ran_time)
        } else if(AdParam.className.indexOf("com.anythink")>=0){
            currentClassName = "com.anythink"
            if(AutoScript.find_element_by_text("剩余")){
                Thread.sleep(2000+ AutoScript.ran_time)
            } else {
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(72, 72)
                Thread.sleep(2000+ AutoScript.ran_time)
                AutoScript.closeWinBySizeOnly(144, 144)
                Thread.sleep(2000+ AutoScript.ran_time)
            }
        } else if(AdParam.className.indexOf("com.kwad.sdk")>=0){
            currentClassName = "com.kwad.sdk"
            Thread.sleep(2000+ AutoScript.ran_time)
            AutoScript.click_element_by_text("继续观看")
            Thread.sleep(1000+ AutoScript.ran_time)
            if(AutoScript.find_element_by_text("获取")){
                AutoScript.closeWinBySizeOnly(36, 36)
            }
            Thread.sleep(2000+ AutoScript.ran_time)
            AutoScript.click_element_by_text("取消")
            Thread.sleep(1000+ AutoScript.ran_time)
            AutoScript.closeWinBySizeOnly(90, 90)
            Thread.sleep(2000+ AutoScript.ran_time)
            AutoScript.click_element_by_text("残忍离开")
            Thread.sleep(1000+ AutoScript.ran_time)
        }
    }
}

